﻿Public Class frmGestionCobranzaCuotas

    Dim utilidades As New Utilidades

    Private Sub cmdSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSalir.Click
        Me.Close()
    End Sub

    Private Sub frmGestionSolicitudPrestamo_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Utilidades.confirmarSalirDelFormulario(e)
    End Sub


    Private Sub frmGestionPrestamo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim myAccesoDatos As New AccesoDatos

        'Cargo Combo de Tipo de Documento
        myAccesoDatos.traer_valores_combo(Me.cmbTipoDocumento, Constantes.Tabla_TIPOS_DOCUMENTOS)

        'Ejecuto una Búsqueda sin Filtros para llenar la Grilla
        Me.buscar(armarWhereConsulta)
    End Sub

    Private Sub cmdBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBuscar.Click
        Dim where As New String("")

        If utilidades.validarRangoDeFechas(txtFechaDesde, txtFechaHasta) = False Then
            Exit Sub
        End If

        Me.buscar(armarWhereConsulta)
    End Sub

    Public Sub buscar(ByVal where As String)
        Dim myAccesoDatos As New AccesoDatos
        Dim myConsulta As String

        myConsulta = "SELECT F.ID AS NRO_FACTURA, CONVERT(VARCHAR(10), F.FECHA, 103) AS FECHA,  "
        myConsulta = myConsulta & "CONVERT(varchar(10), CONVERT(money, F.MONTO_TOTAL)) AS MONTO, "
        myConsulta = myConsulta & "CL.ID AS NRO_CLIENTE, TD.DESCRIPCION AS TIPO_DOCUMENTO, CL.NRO_DOCUMENTO, "
        myConsulta = myConsulta & "(CL.APELLIDO + ', ' + CL.NOMBRE) AS APELLIDO_NOMBRE,  P.ID "
        myConsulta = myConsulta & "FROM PRESTAMOS P INNER JOIN CUOTAS C ON (P.ID = C.ID_PRESTAMO) "
        myConsulta = myConsulta & "INNER JOIN SOLICITUDES_PRESTAMOS S ON (P.ID_SOLICITUD = S.ID) "
        myConsulta = myConsulta & "INNER JOIN CLIENTES CL ON (S.ID_CLIENTE = CL.ID) "
        myConsulta = myConsulta & "INNER JOIN TIPOS_DOCUMENTOS TD ON (CL.ID_TIPO_DOCUMENTO = TD.ID) "
        myConsulta = myConsulta & "INNER JOIN FACTURAS F ON (F.ID_PRESTAMO = C.ID_PRESTAMO AND F.NRO_CUOTA = C.NRO_CUOTA) "
        myConsulta = myConsulta & "WHERE 1 = 1 "


        If Not where.Trim = "" Then
            myConsulta = myConsulta + where
        End If

        Me.gridCobranza.DataSource = myAccesoDatos.ejecutarConsulta(myConsulta)

        Me.habilitarBotonesSegunGrilla()

    End Sub

    Public Function armarWhereConsulta() As String
        Dim where As New String("")
        Dim fechaDesde = txtFechaDesde.Text().Trim()
        Dim fechaHasta = txtFechaHasta.Text().Trim()
        If Not (fechaDesde = "/  /" Or fechaHasta = "/  /") Then

            fechaDesde = utilidades.obtenerFechaFormatoSQLServer2008(fechaDesde)
            fechaHasta = utilidades.obtenerFechaFormatoSQLServer2008(fechaHasta)

            where = where + " AND F.FECHA BETWEEN '" + fechaDesde + "' AND '" + fechaHasta + "'"
        End If

        If cmbTipoDocumento.SelectedIndex > 0 Then
            where = where + " AND CL.ID_TIPO_DOCUMENTO = " + Convert.ToString(cmbTipoDocumento.SelectedValue)
        End If
        If Not (txtNumeroDocumento.Text().Trim() = "") Then
            where = where + " AND CL.NRO_DOCUMENTO = " + txtNumeroDocumento.Text().Trim()
        End If
        If Not (txtNumeroCliente.Text().Trim() = "") Then
            where = where + " AND CL.ID = " + txtNumeroCliente.Text().Trim()
        End If
        If Not (txtPrestamo.Text().Trim() = "") Then
            where = where + " AND P.ID = " + txtPrestamo.Text().Trim()
        End If

        Return where

    End Function


    Public Sub habilitarBotonesSegunGrilla()
        'Dependiendo si la Grilla tiene resultados habilito los botones
        If Me.gridCobranza.RowCount < 1 Then
            Me.cmdReport.Enabled = False
        Else
            Me.cmdReport.Enabled = True
        End If
    End Sub

    Private Sub txtFechaDesde_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtFechaDesde.KeyPress
        utilidades.validarKeyPressNumerico(e)
    End Sub
    Private Sub txtFechaHasta_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtFechaHasta.KeyPress
        utilidades.validarKeyPressNumerico(e)
    End Sub

    Private Sub txtNumeroDocumento_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNumeroDocumento.KeyPress
        utilidades.validarKeyPressNumerico(e)
    End Sub

    Private Sub txtNumeroCliente_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNumeroCliente.KeyPress
        utilidades.validarKeyPressNumerico(e)
    End Sub

    Private Sub txtMonto_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPrestamo.KeyPress
        utilidades.validarKeyPressNumerico(e)
    End Sub

    Private Sub gridSolicitudesPrestamos_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridCobranza.CellDoubleClick
        Me.habilitarBotonesSegunGrilla()
    End Sub

    Private Sub gridSolicitudesPrestamos_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles gridCobranza.CellMouseClick
        Me.habilitarBotonesSegunGrilla()
    End Sub

    Private Sub gridSolicitudesPrestamos_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles gridCobranza.CellMouseDoubleClick
        Me.habilitarBotonesSegunGrilla()
    End Sub

    Private Sub gridSolicitudesPrestamos_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles gridCobranza.CellMouseDown
        Me.habilitarBotonesSegunGrilla()
    End Sub

    Private Sub gridSolicitudesPrestamos_CellMouseLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridCobranza.CellMouseLeave
        Me.habilitarBotonesSegunGrilla()
    End Sub

    Private Sub gridSolicitudesPrestamos_CellMouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles gridCobranza.CellMouseMove
        Me.habilitarBotonesSegunGrilla()
    End Sub

    Private Sub gridSolicitudesPrestamos_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles gridCobranza.Click
        Me.habilitarBotonesSegunGrilla()
    End Sub

    Private Sub gridSolicitudesPrestamos_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gridCobranza.CurrentCellChanged
        Me.habilitarBotonesSegunGrilla()
    End Sub

    Private Sub gridSolicitudesPrestamos_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles gridCobranza.DoubleClick
        Me.habilitarBotonesSegunGrilla()
    End Sub

    Private Sub gridSolicitudesPrestamos_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gridCobranza.LocationChanged
        Me.habilitarBotonesSegunGrilla()
    End Sub

    Private Sub gridSolicitudesPrestamos_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles gridCobranza.LostFocus
        Me.habilitarBotonesSegunGrilla()
    End Sub

    Private Sub gridSolicitudesPrestamos_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles gridCobranza.MouseDoubleClick
        Me.habilitarBotonesSegunGrilla()
    End Sub

    Private Sub gridSolicitudesPrestamos_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles gridCobranza.MouseDown
        Me.habilitarBotonesSegunGrilla()
    End Sub

    Private Sub gridSolicitudesPrestamos_RowLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridCobranza.RowLeave
        Me.habilitarBotonesSegunGrilla()
    End Sub

    Private Sub gridSolicitudesPrestamos_Sorted(ByVal sender As Object, ByVal e As System.EventArgs) Handles gridCobranza.Sorted
        Me.habilitarBotonesSegunGrilla()
    End Sub

    Private Sub cmdReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdReport.Click
        Dim report As New rptCobranzas
        Dim ds As New dsCobranzas
        Dim myAccesoDatos As New AccesoDatos
        Dim myConsulta As String

        myConsulta = "SELECT F.ID AS NRO_FACTURA, CONVERT(VARCHAR(10), F.FECHA, 103) AS FECHA,  "
        myConsulta = myConsulta & "CONVERT(varchar(10), CONVERT(money, F.MONTO_TOTAL)) AS MONTO, "
        myConsulta = myConsulta & "CL.ID AS NRO_CLIENTE, TD.DESCRIPCION AS TIPO_DOCUMENTO, CL.NRO_DOCUMENTO, "
        myConsulta = myConsulta & "(CL.APELLIDO + ', ' + CL.NOMBRE) AS APELLIDO_NOMBRE,  P.ID "
        myConsulta = myConsulta & "FROM PRESTAMOS P INNER JOIN CUOTAS C ON (P.ID = C.ID_PRESTAMO) "
        myConsulta = myConsulta & "INNER JOIN SOLICITUDES_PRESTAMOS S ON (P.ID_SOLICITUD = S.ID) "
        myConsulta = myConsulta & "INNER JOIN CLIENTES CL ON (S.ID_CLIENTE = CL.ID) "
        myConsulta = myConsulta & "INNER JOIN TIPOS_DOCUMENTOS TD ON (CL.ID_TIPO_DOCUMENTO = TD.ID) "
        myConsulta = myConsulta & "INNER JOIN FACTURAS F ON (F.ID_PRESTAMO = C.ID_PRESTAMO AND F.NRO_CUOTA = C.NRO_CUOTA) "
        myConsulta = myConsulta & "WHERE 1 = 1 "

        Dim where As New String("")
        where = armarWhereConsulta()

        If Not where.Trim = "" Then
            myConsulta = myConsulta + where
        End If

        myAccesoDatos.ejecutarConsultaInforme(myConsulta, ds.Cobros)
        Dim row As Data.DataRow = ds.Datos.NewRow()
        Dim fechaDesde = txtFechaDesde.Text().Trim()
        Dim fechaHasta = txtFechaHasta.Text().Trim()
        If Not (fechaDesde = "/  /" Or fechaHasta = "/  /") Then
            row.Item("desde") = fechaDesde
            row.Item("hasta") = fechaHasta
        Else
            row.Item("desde") = " --"
            row.Item("hasta") = " --"
        End If

        ds.Datos.Rows.Add(row)

        report.SetDataSource(ds)

        Dim frmReportCobranza As New frmReportCobranza
        frmReportCobranza.crvCobranza.ReportSource = report
        frmReportCobranza.ShowDialog()
    End Sub
End Class